package me.wangjian.redis.service;

import com.google.common.collect.Lists;
import me.wangjian.redis.model.Company;
import me.wangjian.redis.repository.CompanyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 公司服务类
 * Created by wangjian on 2016/11/17.
 */
@Service
public class CompanyService {

    @Autowired
    CompanyRepository companyRepository;

    @CachePut(value = {"company","companies"},key = "#company.id")
    public Company save(Company company) {
        Company saved = companyRepository.save(company);
        return saved;
    }

    @Cacheable("companies")
    public List<Company> findAll() {
        List<Company> companies = companyRepository.findAll();
        return companies;
    }

    @Cacheable(value = "company",key = "#id")
    public Company findById(Long id) {
        return companyRepository.findOne(id);
    }

    @CacheEvict(value = {"company","companies"},allEntries = true)
    public void remove(Long id) {
        companyRepository.delete(id);
    }
}
